
#include "../common/CRandom.h"
#include "../mainctrl.h"
#include "CAuctionCfg.h"

int CAuctionConfig::AddAuctionGoodsConfig( CAuctionGoodsUnit & stData )
{
    unsigned short usGoodId = stData._usGoodsId;
    if (!IsValidGoods (usGoodId))
    {
        return ERROR_AUCTION_GOODS_ID;
    }

    if (_astGoods.Size() >= MAX_AUCTION_GOODS_NUM)
    {
        return ERROR_AUCTION_GOODS_ID;
    }

    if (_astGoods[usGoodId].HasConfig())
    {
        return ERROR_AUCTION_GOODS_ID;
    }

    _astGoods[usGoodId] = stData;
    _astGoods.m_iUsedCount += 1;

    return 0;
}

int CAuctionConfig::AddAuctionConfig (unsigned char ucAuctionType, CAuctionConfigUnit & stData)
{
    if (!IsValidGoods(stData._usGoodsId))
    {
        return ERROR_AUCTION_CONFIG;
    }

    switch(ucAuctionType)
    {
    case AUCTION_TYPE_BLIND :
        {
            if (_astBlind.Size() >= MAX_AUCTION_BLIND_GOODS)   
            {
                return ERROR_AUCTION_CONFIG;
            }

            _astBlind.AddOneItem(stData);
        }
        break;

    case AUCTION_TYPE_PUBLIC:
        {
            if (_astPublic.Size() >= MAX_AUCTION_PULIC_GOODS)   
            {
                return ERROR_AUCTION_CONFIG;
            }

            _astPublic.AddOneItem(stData);
        }
        break;

    case AUCTION_TYPE_FINAL:
        {
            if (_astPublic.Size() >= MAX_AUCTION_FINAL_GOODS)   
            {
                return ERROR_AUCTION_CONFIG;
            }

            if (stData._ucZoneOpenDay > MAX_AUCTION_FINAL_DAYS)   //首周压轴商品
            {
                return ERROR_AUCTION_CONFIG;
            }

            _astFinal.AddOneItem(stData);
        }
        break;

    default:
        return ERROR_AUCTION_TYPE;
    }

    return 0;
}


CAuctionGoodsUnit &  CAuctionConfig::GetAuctionGoodsConfig(unsigned short usGoodsId)
{
    return _astGoods[usGoodsId];
}

int CAuctionConfig::GenerateAuctionGoods(unsigned char ucAuctionType, CAuctionGoodsUnit & stGoods)
{
    CAuctionConfigUnit stConfig;
    switch(ucAuctionType)
    {
    case AUCTION_TYPE_BLIND :
        {
            int iPos = CT_RANDOM_NUM->GetRand(_astBlind.Size());
            stConfig = _astBlind[iPos];
        }        
        break;

    case AUCTION_TYPE_PUBLIC:
        {
            int iPos = CT_RANDOM_NUM->GetRand(_astPublic.Size());
            stConfig = _astPublic[iPos];
        }
        break;

    case AUCTION_TYPE_FINAL:
        {
            int iZoneOpenDays = CMainCtrl::m_pstMain->GetDaysSinceZoneOpen();
            if (iZoneOpenDays > MAX_AUCTION_FINAL_DAYS)
            {
                iZoneOpenDays = 0;
            }            

            CTArray<unsigned short, MAX_AUCTION_FINAL_GOODS> _astGoods;
            for (int i = 0; i < _astFinal.Size(); i++)
            {
                CAuctionConfigUnit & stUnit = _astFinal[i];
                if (stUnit._ucZoneOpenDay == iZoneOpenDays)
                {
                    _astGoods.AddOneItem(i);  //保存下标
                }
            }

            int iPos = CT_RANDOM_NUM->GetRand(_astGoods.Size());
            stConfig = _astFinal[_astGoods[iPos]];
        }
        break;

    default:
        return ERROR_AUCTION_TYPE;
    }

    if (!IsValidGoods(stConfig._usGoodsId))
    {
        return ERROR_AUCTION_CONFIG;
    }

    stGoods = GetAuctionGoodsConfig(stConfig._usGoodsId);
    if (!stGoods.HasConfig())
    {
        return ERROR_AUCTION_CONFIG;
    }

    return 0;
}
